// 注册组件

import { LcSvgIcon } from '@/components/icon';
import ComponentPreview from '@/components/component-preview/index.vue';
import type { App } from 'vue';

const getComponentNameByPath = (path: string): string => {
  const arrPath = path.split('/');
  const len = arrPath.length;
  const name = arrPath[len - 2];
  if (arrPath[len - 2]) {
    return name;
  }
  return '';
};

export const registerComponent = (app: App<Element>) => {
  const componentsList = [LcSvgIcon, ComponentPreview];
  // lc 下面的组件默认全局注册
  const components = import.meta.glob('./lc/**/index.{vue,tsx}', { eager: true });

  Object.keys(components).forEach((key) => {
    const Comp = (components as Recordable)[key].default;
    const name = Comp.name || getComponentNameByPath(key);
    if (name) {
      app.component(name, Comp);
    }
  });
  componentsList.forEach((Comp: Recordable) => {
    app.component(Comp.name, Comp);
  });
};
